﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyBanVeMayBay.Resources;

namespace QuanLyBanVeMayBay
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void labelX4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void bt_dangnhap_MouseEnter(object sender, EventArgs e)
        {
            this.bt_dangnhap.Size = new System.Drawing.Size(52, 42);
        }

        private void bt_dangnhap_MouseLeave(object sender, EventArgs e)
        {
            this.bt_dangnhap.Size = new System.Drawing.Size(50, 40);
        }

        private void bt_thoat_MouseEnter(object sender, EventArgs e)
        {
            this.bt_thoat.Size = new System.Drawing.Size(42, 44);
        }

        private void bt_thoat_MouseLeave(object sender, EventArgs e)
        {
            this.bt_thoat.Size = new System.Drawing.Size(40, 42);
        }

        private void bt_dangnhap_Click(object sender, EventArgs e)
        {
            if (user_name.Text == "" || pass_word.Text == "")
            {
                MessageBox.Show("Vui lòng điền đầy đủ thông tin", "Lỗi Đăng Nhập",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else
            {
                try
                {
                    xulydulieu conn = new xulydulieu();
                    DataTable taikhoan = new DataTable();
                    string query = "SELECT * From TAIKHOAN WHERE UserName ='" + user_name.Text + "'and PassWord = '" + pass_word.Text+"'";
                    taikhoan = conn.taobang(query);
                    if (taikhoan.Rows.Count == 1)
                    {
                        Form2 f = new Form2();
                        string sql = "select QuyenHan from TAIKHOAN where UserName = '"+ user_name.Text+"'";
                        string quyenhan = conn.taobang(sql).Rows[0][0].ToString();
                        f.Text = "Quản Lý Bán Vé Máy Bay - [Đăng Nhập] : " + user_name.Text;
                        f.tb_doimk_tentaikhoan.Text = user_name.Text;
                        for(int i =0; i < quyenhan.Length;i++)
                        {
                            int a = int.Parse(quyenhan.Substring(0,1));
                            quyenhan = quyenhan.Remove(0, 1);
                            switch (a)
                            {
                                case 1: f.group_phanquyen.Visible = true;
                                    f.group_dstaikhoan.Visible = true; break;
                                case 2: f.tab_chuyen_bay.Visible = true;
                                    f.Tab_nhan_lich_chuyen_bay.Visible = true;
                                    f.tab_quan_ly_chuyen_bay.Visible = true;break;
                                case 3: f.tab_phong_ve.Visible = true;
                                    f.tab_ve_chuyen_bay.Visible = true; break;
                                case 4: f.tab_phong_ve.Visible = true;
                                    f.tab_phieu_dat_cho.Visible = true; break;
                                case 5: f.tab_chuyen_bay.Visible = true;
                                    f.tab_tra_cuu_chuyen_bay.Visible = true;break;
                                case 6: f.tab_bao_cao.Visible = true; break;
                                case 7: f.tab_quy_dinh.Visible = true; break;
                            }
                        }
                        f.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("Sai tên đăng nhập hoặc mật khẩu", "Lỗi", MessageBoxButtons.OK,MessageBoxIcon.Warning);
                        user_name.Clear();
                        pass_word.Clear();
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Kết nối dữ liệu lỗi");
                }

              
            }
        }

        private void bt_thoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void labelX3_Click(object sender, EventArgs e)
        {
   
        }

        private void Form1_Load(object sender, EventArgs e)
        {
       
        }

        private void pass_word_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                bt_dangnhap_Click(Handle,e);
            }
        }
    }
}
